package leetcode;

/**
 * Created by baoyunfeng on 2019/8/5.
 * <p>
 * https://leetcode.com/problems/zigzag-conversion/
 */
public class ZigZag_Conversion {

    public static void main(String args[]) {
        System.out.println(convert("ABCD", 2));
        System.out.print(convert2("ABCD", 2));
    }

    public static String convert(String s, int numRows) {
        char data[][] = new char[s.length()][numRows];
        int index = 0;
        int i = 0, j = 0;
        while (index < s.length()) {
            if (numRows == 1 || i % (1 + numRows - 2) == 0) {
                if (j < numRows) {
                    if (j < 0) j = 0;
                    data[i][j] = s.charAt(index);
                    if (j != numRows - 1) {
                        j++;
                    } else {
                        i++;
                        j--;
                    }
                    index++;
                }
            } else {
                data[i][j] = s.charAt(index);
                i++;
                j--;
                index++;
            }
        }
        String tmp = "";
        for (int h = 0; h < numRows; h++) {
            for (int l = 0; l < s.length(); l++) {
                if (data[l][h] != 0) {
                    tmp += data[l][h];
                }
            }
        }
        return tmp;
    }

    public static String convert2(String s, int numRows) {
        String temp = "";
        if (numRows == 1) {
            return s;
        }
        int tempSize = 2 * numRows - 2;
        int tempCount = s.length() / tempSize + 1;
        int midStart = tempSize / 2;
        for (int i = 0; i < numRows; i++) {
            int gap = midStart - i;
            for (int j = 0; j < tempCount; j++) {
                if (midStart + (j * tempSize) - gap >= 0 && midStart + (j * tempSize) - gap < s.length()) {
                    temp += s.charAt(midStart + (j * tempSize) - gap);
                }
                if (gap != 0 && midStart + (j * tempSize) + gap < midStart + ((j + 1) * tempSize) - gap && midStart +
                        (j * tempSize) + gap < s.length()) {
                    temp += s.charAt(midStart + (j * tempSize) + gap);
                }
            }
        }
        return temp;
    }


    public static String convert3(String s, int numRows) {
        String temp = "";
        if (numRows == 1) {
            return s;
        }
        int tempSize = 2 * numRows - 2;
        for (int i = 0; i < numRows; i++) {

        }


        return temp;
    }
}
